#

| Chapter 5 The C Shell |

## 5.5 Command History csh shell

Previously executed commands are stored in a history list. The C shell lets you access this list so you can verify commands, repeat them, or execute modified versions of them. The history built-in command displays the history list; the predefined variables histchars, history, and savehist also affect the history mechanism. Accessing the history list involves three things:

- Making command substitutions (using ! and ^ ).

- Making argument substitutions (specific words within a command).

- Using modifiers to extract or replace parts of a command or word.

### 5.5.1 Command Substitution

! | Begin a history substitution.
!! | Previous command.
!`N` | Command number `N` in history list.
!-`N` | `N`th command back from current command.
!`string` | Most recent command that starts with `string`.
!?`string`? | Most recent command that contains `string`.
!?`string`?% | Most recent command argument that contains `string`.
!$ | Last argument of previous command.
!!`string` | Previous command, then append `string`.
!`N string` | Command `N`, then append `string`.
!{`s1`}`s2` | Most recent command starting with string `s1`, then append string `s2`.
^`old`^`new`^ | Quick substitution; change string `old` to `new` in previous command; execute modified command.

### 5.5.2 Command Substitution Examples

The following command is assumed:

> `%3` `vi cprogs/01.c ch002 ch03`

Event | Command | Command
--- | --- | ---
Number | Typed | Executed
4 | ^00^0 | vi cprogs/01.c ch02 ch03
5 | nroff !* | nroff cprogs/01.c ch02 ch03
6 | nroff !$ | nroff ch03
7 | !vi | vi cprogs/01.c ch02 ch03
8 | !6 | nroff ch03

Event | Command | Command
--- | --- | ---
Number | Typed | Executed
9 | !?01 | vi cprogs/01.c ch02 ch03
10 | !{nr}.new | nroff ch03.new
11 | !!|lp | nroff ch03.new | lp
12 | more !?pr?% | more cprogs/01.c

### 5.5.3 Word Substitution

Colons may precede any word specifier. After an event number, colons are optional unless shown below:

:0 | Command name.
:`n` | Argument number `n`.
^ | First argument.
$ | Last argument.
:`n`-`m` | Arguments `n` through `m`.
-`m` | Words 0 through `m`; same as :0-`m`.
:`n`- | Arguments `n` through next-to-last.
:`n`* | Arguments `n` through last; same as `n`-$.
* | All arguments; same as ^-$ or 1-$.
# | Current command line up to this point; fairly useless.

### 5.5.4 Word Substitution Examples

The following command is assumed:

> `%13` `cat ch01 ch02 ch03 biblio back`

Event | Command | Command
--- | --- | ---
Number | Typed | Executed
14 | ls !13^ | ls ch01
15 | sort !13:* | sort ch01 ch02 ch03 biblio back
16 | lp !cat:3* | more ch03 biblio back
17 | !cat:0-3 | cat ch01 ch02 ch03
18 | vi !-5:4 | vi biblio

### 5.5.5 History Modifiers

Command and word substitutions can be modified by one or more of the following:

#### 5.5.5.1 Printing, Substitution, and Quoting

:p | Display command but don't execute.
:s/`old`/`new` | Substitute string `new` for `old`, first instance only.
:gs/`old`/`new` | Substitute string `new` for `old`, all instances.
:& | Repeat previous substitution (:s or ^ command), first instance only.
:g& | Repeat previous substitution, all instances.
:q | Quote a wordlist.
:x | Quote separate words.

#### 5.5.5.2 Truncation

:r | Extract the first available pathname root.
:gr | Extract all pathname roots.
:e | Extract the first available pathname extension.
:ge | Extract all pathname extensions.
:h | Extract the first available pathname header.
:gh | Extract all pathname headers.
:t | Extract the first available pathname tail.
:gt | Extract all pathname tails.

### 5.5.6 History Modifier Examples

From above, command number 17 is:

> `%17` `cat ch01 ch02 ch03`

Event | Command | Command
--- | --- | ---
Number | Typed | Executed
19 | !17:s/ch/CH/ | cat CH01 ch02 ch03
20 | !:g& | cat CH01 CH02 CH03
21 | !more:p | more cprogs/01.c (displayed only)
22 | cd !$:h | cd cprogs
23 | vi !mo:$:t | vi 01.c
24 | grep stdio !$ | grep stdio 01.c
25 | ^stdio^include stdio^:q | grep "include stdio" 01.c
26 | nroff !21:t:p | nroff 01.c (is that want I wanted?)
27 | !! | nroff 01.c (execute it)

5.4 Expressions |  | 5.6 Built-in C Shell Commands